package com.qyhl.module_practice.score.mine.shopping;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.qyhl.module_practice.R;
import com.qyhl.module_practice.R2;
import com.qyhl.webtv.basiclib.base.BaseFragment;
import com.qyhl.webtv.basiclib.utils.DateUtils;
import com.qyhl.webtv.basiclib.utils.NetUtil;
import com.qyhl.webtv.commonlib.entity.civilized.PracticeScoreMyShoppingBean;
import com.scwang.smartrefresh.header.MaterialHeader;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.footer.ClassicsFooter;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.weavey.loading.lib.LoadingLayout;
import com.zhy.adapter.recyclerview.CommonAdapter;
import com.zhy.adapter.recyclerview.MultiItemTypeAdapter;
import com.zhy.adapter.recyclerview.base.ViewHolder;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

public class PracticeScoreMyShoppingFragment extends BaseFragment implements PracticeScoreMyShoppingContract.PracticeScoreMyShoppingView {
    @BindView(R2.id.recycle_view)
    RecyclerView recycleView;
    @BindView(R2.id.refresh)
    SmartRefreshLayout refresh;
    @BindView(R2.id.load_mask)
    LoadingLayout loadMask;

    private View view;

    private String volId;

    private int page = 1;

    private List<PracticeScoreMyShoppingBean> mDataList = new ArrayList<>();

    private CommonAdapter commonAdapter;

    private PracticeScoreMyShoppingPresenter mPresenter;

    public void setVolId(String volId) {
        this.volId = volId;
    }


    @Override
    protected View createView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return view = inflater.inflate(R.layout.practice_fragment_score_my_shopping, null);
    }

    @Override
    protected void create() {
        mPresenter = new PracticeScoreMyShoppingPresenter(this);
        loadData();
    }

    @Override
    public void initImmersionBar() {
    }

    @Override
    protected void setListener() {

    }

    @Override
    protected void onFirstVisible() {

    }

    @Override
    protected void onVisible() {

    }

    @Override
    protected void onHide() {

    }

    public static PracticeScoreMyShoppingFragment newInstance(String volId) {
        PracticeScoreMyShoppingFragment fragment = new PracticeScoreMyShoppingFragment();
        fragment.setVolId(volId);
        return fragment;
    }


    /**
     * 加载数据
     */
    private void loadData() {
        //下拉刷新
        initView();
        setListeners();
    }




    private void initView() {
        loadMask.setStatus(LoadingLayout.Loading);

        refresh.setRefreshHeader(new MaterialHeader(getContext()));
        refresh.setRefreshFooter(new ClassicsFooter(getContext()));
        refresh.setEnableLoadMore(true);

        recycleView.setLayoutManager(new LinearLayoutManager(getContext()));
        recycleView.setAdapter(commonAdapter = new CommonAdapter<PracticeScoreMyShoppingBean>(getContext(), R.layout.practice_item_score_my_shopping, mDataList) {
            @Override
            protected void convert(ViewHolder holder, final PracticeScoreMyShoppingBean bean, int position) {
                TextView state = holder.getView(R.id.state);
                if (bean.isUseState()) {
                    state.setEnabled(false);
                    state.setText("已领取");
                    holder.setVisible(R.id.exchange_btn, false);
                } else {
                    state.setEnabled(true);
                    state.setText("未领取");
                    holder.setVisible(R.id.exchange_btn, true);
                }

                ImageView cover = holder.getView(R.id.cover);
                Glide.with(PracticeScoreMyShoppingFragment.this)
                        .load(bean.getItem().getItemPic())
                        .apply(new RequestOptions().placeholder(R.drawable.cover_normal_default).error(R.drawable.cover_normal_default))
                        .into(cover);

                holder.setText(R.id.title, bean.getItem().getItemName());

                holder.setText(R.id.score, bean.getPoint() + "");

                holder.setText(R.id.date, DateUtils.practiceShoppingFormat(bean.getCreateTime()));

                holder.setOnClickListener(R.id.exchange_btn, new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        PracticeScoreShoppingPop scoreShoppingPop = new PracticeScoreShoppingPop(getActivity(), bean);
                        scoreShoppingPop.setDismissWhenTouchOuside(true);
                        scoreShoppingPop.showPopupWindow();
                    }
                });
            }
        });

        mPresenter.getList(volId, page + "");
    }

     private void setListeners() {
        loadMask.setOnReloadListener(new LoadingLayout.OnReloadListener() {
            @Override
            public void onReload(View v) {
                loadMask.setReloadButtonText("加载中...");
                page = 1;
                mPresenter.getList(volId, page + "");
            }
        });

        refresh.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                page = 1;
                mPresenter.getList(volId, page + "");
            }
        });

        refresh.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                mPresenter.getList(volId, page + "");
            }
        });

        commonAdapter.setOnItemClickListener(new MultiItemTypeAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View view, RecyclerView.ViewHolder holder, int position) {
                if (mDataList.get(position).isUseState()) {
                    PracticeScoreShoppingPop scoreShoppingPop = new PracticeScoreShoppingPop(getActivity(), mDataList.get(position));
                    scoreShoppingPop.setDismissWhenTouchOuside(true);
                    scoreShoppingPop.showPopupWindow();
                }
            }

            @Override
            public boolean onItemLongClick(View view, RecyclerView.ViewHolder holder, int position) {
                return false;
            }
        });
    }


    @Override
    public void setList(List<PracticeScoreMyShoppingBean> beanList, boolean isMore) {
        loadMask.setReloadButtonText("点击重试~");
        if (isMore) {
            refresh.finishLoadMore();
        } else {
            loadMask.setStatus(LoadingLayout.Success);
            refresh.finishRefresh();
            mDataList.clear();
        }
        page++;
        mDataList.addAll(beanList);
        commonAdapter.notifyDataSetChanged();
    }

    @Override
    public void setError(String msg, boolean isMore) {
        loadMask.setReloadButtonText("点击重试~");
        if (isMore) {
            refresh.finishLoadMore();
            if (!NetUtil.isNetworkAvalible(getContext())) {
                showToast("网络异常，请检查您的网络！", 4);
            } else {
                showToast(msg, 4);
            }
        } else {
            refresh.finishRefresh();
            loadMask.setStatus(LoadingLayout.Error);
            if (!NetUtil.isNetworkAvalible(getContext())) {
                loadMask.setErrorImage(R.drawable.error_network);
                loadMask.setErrorText("网络异常，请检查您的网络！");
            } else if (msg.contains("暂无")) {
                loadMask.setErrorImage(R.drawable.empty_content);
                loadMask.setErrorText(msg);
            } else {
                loadMask.setErrorImage(R.drawable.error_content);
                loadMask.setErrorText(msg);
            }
        }
    }
}
